package edu.moravian.UI.game;

import edu.moravian.UI.entity.BallEntityView;
import edu.moravian.entity.BallEntity;
import edu.moravian.entity.BallEntityManager;
import edu.moravian.math.Rand;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

/**
 * The view (visual representation) of the bouncing ball game
 *
 * @author Elliot
 */
public class BouncingBallGameView
{
    private WorldDescriptor world;
    private List<BallEntityView> balls;
    
    private boolean displayEpsilon;

    private BallEntityManager ballsModel;
    
    /**
     * Create a new view based on the world description and the balls in the game.
     * The balls will be assigned a random color and will be draw according to their
     * dimensions relative to the world dimensions
     *
     * @param world the description of the world
     * @param ballsModel the ball manager that has all the ball models
     */
    public BouncingBallGameView(WorldDescriptor world, BallEntityManager ballsModel)
    {
        displayEpsilon = false;
        this.world = world;
        this.ballsModel = ballsModel;

        //Assign a random color for each ball. This relies on the fact that balls are 
        //not getting deleted or added to the game

        balls = new ArrayList<BallEntityView>();

        for (BallEntity ball : ballsModel.getBalls())
        {
            Color color = new Color(Rand.inRange(0, 255), Rand.inRange(0, 255), Rand.inRange(0, 255));
            balls.add(new edu.moravian.UI.entity.BallEntityView(ball, color));
        }
    }




    /**
     * Render the world on the specified canvas
     *
     * @param w2d the canvas to draw on
     */
    public void draw(World2D w2d)
    {
        //draw the background 
        w2d.setColor(Color.WHITE);
        w2d.drawCenteredRectangle(world.getCenter(), world.getWidth(), world.getHeight());

        //draw each ball, if balls were getting added/deleted we would 
        //need to a more flexible way of maintaing the view
        for (BallEntityView ball : balls)
        {
            ball.draw(w2d);
        }
        
        if(displayEpsilon)
        {
            w2d.setColor(Color.BLACK);
            int fontSize = 25;
            Font oldFont = w2d.getFont();
            Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), fontSize);
            w2d.setFont(newFont);
            
            w2d.drawRightAlignedString(String.format("Epsilon: %.2f", ballsModel.getElasticity()), world.getUpperRight());
            w2d.setFont(oldFont);
        }
    }
    
    public void changeDisplayEpsilon()
    {
        displayEpsilon = !displayEpsilon;
    }
          



}



